package com.xiaohai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaohai.exception.CustomException;
import com.xiaohai.projo.Category;
import com.xiaohai.projo.Dish;
import com.xiaohai.projo.Setmeal;
import com.xiaohai.service.CategoryService;
import com.xiaohai.mapper.CategoryMapper;
import com.xiaohai.service.DishService;
import com.xiaohai.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 36343
 * @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
 * @createDate 2024-01-16 08:54:14
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(long id) {
        // 查询相关联的菜品
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        dishQueryWrapper.eq(Dish::getCategoryId, id);
        long dishCount = dishService.count(dishQueryWrapper);

        if (dishCount > 0) {
            throw new CustomException("当前分类象关联了菜品，不能被删除");
        }
        // 查询关联的套餐
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<>();
        setmealQueryWrapper.eq(Setmeal::getCategoryId, id);
        long SetmealCount = setmealService.count(setmealQueryWrapper);
        if (SetmealCount > 0) {
            throw new CustomException("当前分类象关联了套餐，不能被删除");
        }
    }
}




